﻿CREATE FUNCTION [dbo].[hist_TrackerRequest] 
(	@TrackerRequestID varchar(10)
)RETURNS TABLE
AS
RETURN 

SELECT     TOP (100) PERCENT mwf.MenuPath AS Status, tech.Name AS Installer, htr.VIN, htr.DealerID, htr.Hist_UserName AS Who, 
                      dbo.Date_Format(MIN(htr.Hist_DateStamp), '5.1') AS [When], MIN(htr.HID) AS HID, LEFT(t_Dealer.Name,25) AS DealerName
FROM         vhist_t_TrackerRequest AS htr INNER JOIN
                      v_Menu_WorkFlow AS mwf ON htr.StatusID = mwf.MenuID INNER JOIN
                      v_Technician AS tech ON htr.TechnicianID = tech.ID INNER JOIN
                      t_Dealer ON htr.DealerID = t_Dealer.ID
WHERE     (htr.ID = @TrackerRequestID)
GROUP BY mwf.MenuPath, tech.Name, htr.VIN, htr.DealerID, htr.Hist_UserName, t_Dealer.Name
ORDER BY MAX(htr.HID) DESC



